package com.fengwk.support.serialization;

import java.io.Serializable;

import com.fengwk.support.util.IoUtils;
import com.fengwk.support.util.ReflectUtils;

/**
 * 序列化包装,以实现跨域序列化
 */
public class SerializationWrap implements Serializable {

	private static final long serialVersionUID = 5497443052368875722L;

	private byte[] wrapObjBytes;// 包装对象bytes
	private String wrapObjClassName;// 包装对象ClassName

	public <O extends Serializable> SerializationWrap(O obj) {
		this.wrapObjClassName = obj.getClass().getName();
		this.wrapObjBytes = IoUtils.objectToBytes(obj);
	}

	/**
	 * 获取包装对象
	 * 
	 * @return 根据包装对象字节码是否存在返回 包装对象 或 bytes
	 */
	public Object getWrapObj() {
		if(ReflectUtils.hasClass(this.wrapObjClassName)) {
			return IoUtils.bytesToObject(wrapObjBytes);
		}
		return null;
	}

}
